package com.hgwz.spring.api;

import com.hgwz.spring.api.params.MenuBody;
import com.hgwz.spring.api.params.ReqBody;
import com.hgwz.spring.entity.Menu;
import com.hgwz.spring.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "练习接口")
@RestController
@RequestMapping("/")
public class Exercise {

    @Resource
    private MenuService menuService;

    @GetMapping("/")
    @ApiOperation(value = "问候接口", notes = "程序启动之后该接口即可使用")
    public String sayHello() {
        System.out.println("Health check!");
        return "Hi, 这是用于学习的SpringBoot站点。";
    }

    @GetMapping("/menus")
    public List<Menu> viewAllMenu() {
        List<Menu> menus = menuService.getAllMenus();
        System.out.println(menus);
        return menus;
    }

    @DeleteMapping("/delmenu")
    public String delMenu(@RequestParam(required = true) @ApiParam(value = "菜单编号") String mId){
        return "" + menuService.delMenuItem(new Integer(mId).intValue());
    }

    @PostMapping("/addmenu")
    public String addNewMenu(@RequestBody MenuBody menuBody) {
        int result = menuService.addNewMenu(menuBody.getCid(), menuBody.getMname(), menuBody.getPrice());
        System.out.println("Add menu " + result + " new menu item.");
        return "" + result;
    }

    @GetMapping("/params")
    @ApiOperation(value = "参数输入接口", notes = "传递Get 参数")
    public String params(@RequestParam(required = true) @ApiParam(value = "参数1") String key1,
                         @RequestParam(required = false) @ApiParam(value = "参数2") String key2,
                         @RequestParam(required = false) @ApiParam(value = "参数3") String key3) {
        String rt = "Key1 = " + key1 + "\nKey2 = " + key2 + "\nKey3 = " + key3;
        System.out.println(rt);
        return rt;
    }

    @GetMapping("/pathvariable/{id1}/{id2}/no")
    @ApiOperation(value = "PathVariable 接口", notes = "传递路径参数")
    public String pathVariable(@ApiParam(value = "ID号 - 1") @PathVariable("id1") String id1
            , @ApiParam(value = "ID号 - 2") @PathVariable("id2") String id2) {

        String rt = "id1 = " + id1 + "\nid2 = " + id2;
        System.out.println(rt);
        return rt;
    }

    @PostMapping("/post")
    @ApiOperation(value = "Post接口", notes = "尝试Post接口")
    public String helloPost(@RequestBody @Valid ReqBody requestBody) {
        String rt = "param1 = " + requestBody.getParam1() + "\nparam2 = " + requestBody.getParam2();
        System.out.println(rt);
        return rt;
    }
}
